window.onload = function () {
	makeSortable();
}

function makeSortable()
{
	Sortable.create("list", {
		tree:true,
		dropOnEmpty:true,
		containment:["list"],
		ghosting:false,
		constraint:false,
		overlap: 'horizontal'
	});
}

function addSerialize()
{
	poststring = Sortable.serialize('list');
	new Insertion.Bottom('form', '<input type="hidden" name="serialize" value="'+poststring+'" />');
	hideGeneration();
}

function selectAll()
{
	var checkbox = document.getElementsByClassName("check");
	checkbox.each(function(el){ el.checked = true; });
	return false;
}

function deselectAll()
{
	var checkbox = document.getElementsByClassName("check");
	checkbox.each(function(el){ el.checked = false; });
	return false;
}

function showSection() { 
	hideSubSection();
	hideChooseStyle();
	hideAddStyle();
	hideGeneration();
	Element.setStyle('addSection', {display:'block'} ); 
	Effect.Pulsate('addSection', {pulses: 1, from: 0.1});
}
function hideSection() { Element.setStyle('addSection', {display:'none'} ); }

function showSubSection() { 
	hideSection();
	hideAddStyle();
	hideChooseStyle();
	hideGeneration();
	Element.setStyle('addSubSection', {display:'block'} ); 
	Effect.Pulsate('addSubSection', {pulses: 1, from: 0.1});
}
function hideSubSection() { Element.setStyle('addSubSection', {display:'none'} ); }

function showAddStyle() { 
	hideSubSection();
	hideChooseStyle();
	hideSection();
	hideGeneration();
	Element.setStyle('addStyle', {display:'block'} ); 
	Effect.Pulsate('addStyle', {pulses: 1, from: 0.1});
}
function hideAddStyle() { Element.setStyle('addStyle', {display:'none'} ); }

function showChooseStyle() {
	hideSubSection();
	hideSection();
	hideAddStyle();
	hideGeneration();

	Element.setStyle('chooseStyle', {display:'block'} ); 
	Effect.Pulsate('chooseStyle', {pulses: 1, from: 0.1});
}
function hideChooseStyle() { Element.setStyle('chooseStyle', {display:'none'} ); }

function showGeneration() {
	hideSubSection();
	hideChooseStyle();
	hideSection();
	hideAddStyle();

	Element.setStyle('generatePdf', {display:'block'} ); 
	Effect.Pulsate('generatePdf', {pulses: 1, from: 0.1});
}
function hideGeneration() {Element.setStyle('generatePdf', {display:'none'} ); }

function loadStyles() {

	new Ajax.Request(base_url+'manager/deleteFrame', opt);

}

function insertSection()
{
	hideSection();
	
	var title = $('newSection').value;

	var frame = '<li class="frame_section" id="frame_S'+title+'" ><h3>Section</h3><h2>'+title+'</h2> <a href="#nogo" class="remove" onclick="Element.remove(\'frame_S'+title+'\')">Supprimer</a> </li>';
	new Insertion.Top('list', frame);
	makeSortable();
}

function insertSubSection()
{
	hideSubSection();
	
	var title = $('newSubSection').value;

	var frame = '<li class="frame_section" id="frame_B'+title+'" > \
		<h3>Sous Section</h3> \
		<h2>'+title+'</h2> \
		<a href="#nogo" class="remove" onclick="Element.remove(\'frame_B'+title+'\')">Supprimer</a> \
		</li>';
	new Insertion.Top('list', frame);
	makeSortable();
}

function suprFrame()
{
	var idCol = $('idCol').value;
	
	var param = "idCol="+idCol+"&frames=";
	
	if (confirm('Etes vous sure de vouloir supprimer ce(s) frame(s)'))
	{
		var checkbox = document.getElementsByClassName("check");
		checkbox.each(function(el){
			if(el.checked == true) param += el.name+"_";
		});
		
		var opt = {
		    method: 'post',						// Use POST
		    postBody: param,				    // Send this lovely data
		    onSuccess: function(t) {		    // Handle successful response
		        //alert(t.responseText);
				checkbox.each(function(el){
					if(el.checked == true)
					{
						Effect.Fade('frame_'+el.name, { transition: Effect.Transitions.wobble });
						Element.remove('frame_'+el.name);
					}
				});
		 		
		    },
		    on404: function(t) {			    // Handle 404
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {		    // Handle other errors
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
		new Ajax.Request(base_url+'manager/deleteFrame', opt);
		
		return false;
	}
	else return false;
}

